package org.osomit.sacct.client.impl;

import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

import org.osomit.sacct.connector.socket.impl.SocketConnectorProviderImpl;
import org.osomit.sacct.io.impl.JSONClientReaderWriter;
import org.osomit.sacct.handler.impl.ClientServiceHandlerImpl;
import org.osomit.sacct.session.impl.SessionContextHolderImpl;
import org.osomit.sacct.entity.ApplicationAccount;

import java.util.List;

/**
 * Manual Test, need to run SAcct server to make it work
 *
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 30, 2009
 * 
 */
public class AccountClientImpl_MT {
    protected static AccountClientImpl client;

    @BeforeClass
    public static void setUp(){
        client = new AccountClientImpl();
        client.setClientId("server");
        client.setConnectorProvider(new SocketConnectorProviderImpl());
        client.setReaderWriter(new JSONClientReaderWriter());
        client.setServiceHandler(new ClientServiceHandlerImpl());
        client.setSessionContextHolder(new SessionContextHolderImpl());
        client.setUseOTP(true);
    }

    @Test
    public void testSayHello(){
        //Assume Account Server running on localhost port 9000
        client.connect("localhost", 9000);
        String hello = client.sayHello();
        System.out.println(hello);
        assertEquals("Hello, server", hello);
        client.disconnect();
    }

    @Test
    public void testFetchAllAccounts(){
        client.connect("localhost", 9000);

        List<ApplicationAccount> accounts =  client.fetchAllAccounts();
        if(accounts != null){
            for(ApplicationAccount account: accounts){
                System.out.println(account.toJSON().toString());
            }
        }
        client.disconnect();
    }

    @Test
    public void loadTest() {
        for (int i = 0; i < 10; i++) {
            client.connect("localhost", 9000);

            for (int j = 0; j < 10; j++) {
                List<ApplicationAccount> accounts = client.fetchAllAccounts();
                if (accounts != null) {
                    for (ApplicationAccount account : accounts) {
                        System.out.println(account.toJSON().toString());
                        client.fetchAccount(account.getName());
                    }
                }
            }

            client.disconnect();
        }
    }
}
